<?php
/**
 * 会员组
**/
include("../includes/common.php");
$title='会员组';
include './head.php';
if($islogin==1){}else loginmsg('请登录后再操作！');
?>

<div class="layui-fluid">
    <div class="layui-card">
        <div id="divLoading">
          <div class="layui-card-header"><?=$title;?></div>
            <div class="layui-card-body layui-form">
            	<!-- 表格工具栏 -->
            	<form class="layui-form toolbar">
            		<div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">选择类型</label>
                            <div class="layui-input-block">
                                <select name="type">
                                    <option value="0">全部</option>
                                    <option value="1">ID</option>
                                    <option value="2">名称</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
            				<label class="layui-form-label w-auto">搜索数据：</label>
            				<div class="layui-input-inline mr0">
            					<input type="text" name="kw" class="layui-input" placeholder="请输入你所要搜索的内容！" />
            				</div>
            			</div>
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">搜索类别</label>
                            <div class="layui-input-block">
                                <select name="method">
                                    <option value="0">精确搜索</option>
                                    <option value="1">模糊搜索</option>
                                </select>
                            </div>
                        </div>
            			<div class="layui-inline">
            				<button class="layui-btn icon-btn" lay-filter="roleTbSearch" lay-submit>
            					<i class="layui-icon">&#xe615;</i>搜索
            				</button>
            			</div>
            		</div>
            	</form>
            <!-- 数据表格 -->
            <table class="layui-hide" id="group_list" lay-filter="group_list"></table>
            <script id="table_button" type="text/html">
                <a lay-event="edit" class="layui-btn layui-btn layui-btn-xs"><i class="layui-icon layui-icon-edit"></i>修改</a>
                <a lay-event="del" class="layui-btn layui-btn-danger layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
            </div>
        </div>
    </div>
</div>
<?php include 'foot.php';?>
<!-- 添加会员组 -->
<script type="text/html" id="addgroup">
    <form class="layui-form model-form" id="formAdvForm" lay-filter="addform">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="name" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">云存储:</label>
            <div class="layui-input-block">
            <select name="storage_type" class="layui-input" default="storage_type">
                <option value="oss">阿里云</option>
                <option value="qcloud">腾讯云</option>
                <option value="local">本地存储</option>
                <option value="qiniu">七牛云</option>
                <option value="upyun">又拍云</option>
                <option value="obs">华为云</option>
            </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">售价</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="price" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">有效期(月)</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="endtime" placeholder="0为永久">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否上架</label>
            <div class="layui-input-block">
                <select name="active" class="layui-input">
                    <option value="0">下架</option>
                    <option value="1">上架</option>
                </select>
            </div>
        </div>
    </form>
</script>
<!-- 修改会员组 -->
<script type="text/html" id="editgroup">
    <form class="layui-form model-form" id="formAdvForm" lay-filter="editgroupform">
    <input type="hidden" name="id" value=""/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="name" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">云存储:</label>
            <div class="layui-input-block">
            <select name="storage_type" class="layui-input" default="storage_type">
                <option value="oss">阿里云</option>
                <option value="qcloud">腾讯云</option>
                <option value="local">本地存储</option>
                <option value="qiniu">七牛云</option>
                <option value="upyun">又拍云</option>
                <option value="obs">华为云</option>
            </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">售价</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="price" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">有效期(月)</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="endtime" placeholder="0为永久">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否上架</label>
            <div class="layui-input-block">
                <select name="active" class="layui-input">
                    <option value="0">下架</option>
                    <option value="1">上架</option>
                </select>
            </div>
        </div>
    </form>
</script>
<script type="text/javascript">
    layui.use(['jquery', 'layer', 'notice', 'form', 'table', 'admin', 'element', 'xmSelect', 'tagsInput'],function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var notice = layui.notice;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var tagsInput = layui.tagsInput;


        var insTb = table.render({
            elem: '#group_list',
            url: './ajax.php?act=group',
            text: {
                none: '哇哦没发现数据哟 T^T ！' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            },
            page: true,
            skin: 'line',
            toolbar: ['<p>', '<button class="layui-btn layui-btn-sm layui-btn-green" lay-event="add"><i class="layui-icon layui-icon-addition" style="font-size:20px;"></i>添加</button>&nbsp;', '</p>'].join(''),
            cellMinWidth: 100,
            limit: 10,
            cols: [[
                {
                    field: 'id',
                    title: '编号',
                    templet: function(data) {
                        return '<span class="layui-badge layui-badge-blue">'+data.id+'</span>';
                    },
				    align: 'center',
				    width: 80,
                    sort: true
                },
                {
                    field: 'name',
                    title: '名称',
				    align: 'center',
                    sort: false
                },
                {
                    field: 'storage_type',
                    title: '云存储',
				    align: 'center',
                    sort: false
                },
                {
                    field: 'price',
                    title: '售价(￥)',
				    align: 'center',
                    sort: false
                },
                {
                    field: 'endtime',
                    title: '有效期(月)',
                    templet: function(data) {
                        if(data.endtime == '0'){
                            return '<span class="layui-badge layui-badge-green">永久</span>';
                        } else {
                            return '<span class="layui-badge layui-badge-blue">'+data.endtime+'月</span>';
                        }
                    },
				    align: 'center',
                    sort: false
                },
                {
                    field: 'active',
                    title: '状态',
                    templet: function(data) {
                        if (data.active == 1) {
                            return '<input type="checkbox" name="close" lay-skin="switch" value="' + data.id + '" lay-filter="switchApp" lay-text="上架|下架" checked/>';
                        } else {
                            return '<input type="checkbox" name="close" lay-skin="switch" value="' + data.id + '" lay-filter="switchApp" lay-text="上架|下架" >';
                        }
                    },
					align: 'center',
                    sort: false
                },
                {
                    field: "action",
                    title: "功能操作",
                    toolbar: "#table_button",
				    align: 'center',
				    width: 220
                }]]
        });
        
        /* 表格搜索 */
        form.on('submit(roleTbSearch)',function(data) {
            var ii = layer.msg("正在查找数据……", {
                icon: 16
            });
            insTb.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            layer.close(ii);
            return false;
        });

        /* 修改状态 */
        form.on('switch(switchApp)',function(data) {
            $.ajax({
                url: './ajax.php?act=switchgroup&id=' + data.value,
                dataType: 'json',
                success: function(data) {
                    if (data.code == 0) {
                        notice.msg(data.msg, {
                            icon: 1
                        });
                    } else {
                        notice.msg(data.msg, {
                            icon: 2
                        });
                    }
                }
            });
        });

        /* 批量删除 */
        table.on('toolbar(group_list)', function(obj){
            if(obj.event === "add"){
                admin.open({
                    type: 1,
                    title: '添加会员组',
                    btn: ['添加', '取消'],
                    content: $('#addgroup').html(),
                    success: function (layero, dIndex) {
                        form.render();
                        admin.modelForm(layero, 'add', 'addform');
                        
                    
                        form.on('submit(add)', function (data) {
                            notice.msg('正在添加中..', {icon: 4, close: true});
                            $.ajax({
                                type: "POST",
                                url: "ajax.php?act=addgroup",
                                data: data.field,
                                dataType: "json",
                                success: function(data) {
                                    notice.destroy();
                                    if (data.code == 0) {
                                        insTb.reload();
                                        layer.msg(data.msg, {
            						        icon: 6
            					        });
                                    } else {
                                        layer.msg(data.msg, {
            						        icon: 5
            					        });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            }
        });

        /* 表格工具栏 */
        table.on('tool(group_list)', function(obj) {
            if (obj.event === "del") {
                layer.confirm('你确实要删除此数据吗？', {
                        btn: ['确定', '取消'],
                        closebtn: 0,
                        icon: 3
                    },
                    function(index) {
                        $.ajax({
                            type: "POST",
                            url: "ajax.php?act=delgroup",
                            data: {
                                id: obj.data.id
                            },
                            dataType: "json",
                            success: function(data) {
                                if (data.code == 0) {
                                    notice.success({
                                        title: '消息通知',
                                        message: data.msg,
                                        position: 'topCenter',
                                        audio: ''
                                    });
                                    insTb.reload();
                                } else {
                                    notice.error({
                                        title: '消息通知',
                                        message: data.msg,
                                        position: 'topCenter',
                                        audio: ''
                                    });
                                }
                            }
                        });
                        layer.close(index);
                    },
                function() {});
            } else if(obj.event === 'edit') { // 更改会员组
                admin.open({
                    type: 1,
                    title: '更改会员组',
                    content: $('#editgroup').html(),
                    btn: ['修改', '取消'],
                    success: function (layero, dIndex) {
                        form.val('editgroupform',obj.data);
                        admin.modelForm(layero, 'editgroup', 'editgroupform');
                        
                        $('#demoTagsInput1').tagsInput();
                        
                        fetch('ajax.php?act=recode1')
                          .then(response => response.json())
                          .then(data => {
                            // 在这里处理后端返回的数据
                            // 将数据传递给xmSelect.render()方法
                            xmSelect.render({
                              el: '#userrecode',
                              toolbar: {show: true},
                              data: data.data
                            });
                          })
                          .catch(error => {
                            // 处理错误情况
                            console.error('Error:', error);
                          });
                          
                        form.on('submit(editgroup)', function (data) {
                            notice.msg('正在执行中..', {icon: 4, close: true});
                            $.ajax({
                                type: "POST",
                                url: "ajax.php?act=editgroup",
                                data: data.field,
                                dataType: "json",
                                success: function(data) {
                                    notice.destroy();
                                    if (data.code == 0) {
                                        insTb.reload();
                                        layer.msg(data.msg, {
                                            icon: 6
                                        });
                                    } else {
                                        layer.msg(data.msg, {
                                            icon: 5
                                        });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            }
        });

    });
</script>